package org.techmore.utils;

import java.io.IOException;

import org.techmore.R;
import org.techmore.zxing.client.android.PreferencesActivity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
//import android.os.Vibrator;
import android.preference.PreferenceManager;

public class BeeManager {

	public static final String KEY_PLAY_BEEP = "preferences_play_beep";
	public static final String KEY_VIBRATE = "preferences_vibrate";
	public static final String KEY_COPY_TO_CLIPBOARD = "preferences_copy_to_clipboard";
//	private static final float BEEP_VOLUME = 0.10f;
//	private static final long VIBRATE_DURATION = 200L;

	private boolean playBeep;
//	private boolean vibrate;
	
	private static BeeManager beeManager = null;
	private MediaPlayer okMediaPlayer = null;
	private MediaPlayer stopMediaPlayer = null;
	
	
	private final OnCompletionListener beepListener = new OnCompletionListener() {
	    public void onCompletion(MediaPlayer mediaPlayer) {
	      mediaPlayer.seekTo(0);
	    }
	  };
	
	public static BeeManager getInstance(Activity activity, Context context){
		if(null != beeManager){
			return  beeManager;
		}else{
			beeManager = new BeeManager(activity, context);
			return beeManager;
		}
	}

	public BeeManager(Activity activity, Context context) {
		confirmSoundSystem(activity, context);
		initOKBeepSound(activity);
		initStopBeepSound(activity);
	}

	private void confirmSoundSystem(Activity activity, Context context) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		playBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true);
		if (playBeep) {
			// See if sound settings overrides this
			AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
			if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
				playBeep = false;
			}
		}
//		vibrate = prefs.getBoolean(PreferencesActivity.KEY_VIBRATE, false);
	}
	
	private void initOKBeepSound(Activity activity) {
	    if (playBeep && okMediaPlayer == null) {
	      // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
	      // so we now play on the music stream.
	    	activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
	    	okMediaPlayer = new MediaPlayer();
	    	okMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	    	okMediaPlayer.setOnCompletionListener(beepListener);

	    	AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.ok);
	    	
	      try {
	    	  okMediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(),file.getLength());
	    	  file.close();
//	    	  okMediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
	    	  okMediaPlayer.prepare();
	      } catch (IOException e) {
	    	  okMediaPlayer = null;
	      }
	    }
	  }
	
	private void initStopBeepSound(Activity activity) {
	    if (playBeep && stopMediaPlayer == null) {
	      // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
	      // so we now play on the music stream.
	    	activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
	    	stopMediaPlayer = new MediaPlayer();
	    	stopMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	    	stopMediaPlayer.setOnCompletionListener(beepListener);

	    	AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.stop);
	    	
	      try {
	    	  stopMediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(),file.getLength());
	    	  file.close();
//	    	  stopMediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
	    	  stopMediaPlayer.prepare();
	      } catch (IOException e) {
	    	  stopMediaPlayer = null;
	      }
	    }
	  }
	
	  public void playOKBeepSoundAndVibrate(Activity activity) {
	    if (playBeep && okMediaPlayer != null) {
	    	okMediaPlayer.start();
	    }
//	    if (vibrate) {
//	      Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
//	      vibrator.vibrate(VIBRATE_DURATION);
//	    }
	  }
	  
	  public void playStopBeepSoundAndVibrate(Activity activity) {
		    if (playBeep && stopMediaPlayer != null) {
		    	stopMediaPlayer.start();
		    }
//		    if (vibrate) {
//		      Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
//		      vibrator.vibrate(VIBRATE_DURATION);
//		    }
		  }

}
